Estamos superando las limitaciones de los modelos lineales, que tienen dificultades para clasificar datos que no son separables por una línea recta. Hoy aplicamos el flujo de trabajo de PyTorch para construir un Red Neuronal Profunda (DNN) capaz de aprender fronteras de decisión complejas y no lineales esenciales para tareas reales de clasificación.
1. Visualización de la Necesidad de Datos No Lineales
Nuestro primer paso es crear un conjunto de datos sintéticos desafiantes, como la distribución de dos medias, para demostrar visualmente por qué los modelos lineales simples fallan. Esta configuración nos obliga a utilizar arquitecturas profundas para aproximar la curva intrincada necesaria que separa las clases.
Propiedades de los Datos
- Estructura de los Datos: Características de datos sintéticos (por ejemplo, $1000 \times 2$ para $1000$ muestras con 2 características).
- Tipo de Salida: Un valor de probabilidad único, generalmente
torch.float32, que representa la membresía de clase. - Objetivo: Crear una frontera de decisión curva mediante cálculos en capas.
El Poder de las Activaciones No Lineales
El principio fundamental de las DNN es la introducción de no linealidad en las capas ocultas mediante funciones como ReLU. Sin estas, apilar capas simplemente daría lugar a un modelo lineal grande, independientemente de la profundidad.
TERMINALbash — classification-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR Live
Run code to inspect active tensors
Question 1
What is the primary purpose of the ReLU activation function in a hidden layer?
Question 2
Which activation function is required in the output layer for a binary classification task?
Question 3
Which loss function corresponds directly to a binary classification problem using a Sigmoid output?
Challenge: Designing the Core Architecture
Integrating architectural components for non-linear learning.
You must build a
nn.Module for the two-moons task. Input features: 2. Output classes: 1 (probability).
Step 1
Describe the flow of computation for a single hidden layer in this DNN.
Solution:
Input $\to$ Linear Layer (Weight Matrix) $\to$ ReLU Activation $\to$ Output to Next Layer.
Input $\to$ Linear Layer (Weight Matrix) $\to$ ReLU Activation $\to$ Output to Next Layer.
Step 2
What must the final layer size be if the input shape is $(N, 2)$ and we use BCE loss?
Solution:
The output layer must have size $(N, 1)$ to produce a single probability score per sample, matching the label shape.
The output layer must have size $(N, 1)$ to produce a single probability score per sample, matching the label shape.